﻿namespace Dng.TraficTracking
{
    using System;
    using System.Configuration;
    using System.Web;

    public class Global
    {
        private decimal _DngCountRate = 1M;
        private long _DngCurrentVisited = 0L;
        private double _DngRefreshRate = 2.0;
        private double _DngRefreshTracking = 1.0;
        private string _DngTracficTrackingSummaryListName = string.Empty;
        private string _DngVisitTrackingListName = string.Empty;
        private string _SiteUrl = string.Empty;
        private string _TraficTrackingListName = string.Empty;
        private static Global mInstance = null;
        private static object SyncRoot = new object();

        private Global()
        {
        }

        public string CompactUrl(HttpRequest request)
        {
            string result = request.Url.ToString().ToLower();
            int index = result.IndexOf(".aspx");
            if (index < 0)
            {
                index = result.IndexOf("/?");
                if (index > -1)
                {
                    result = result.Substring(0, index + 1);
                }
            }
            else
            {
                result = result.Substring(0, index + 5);
            }
            if (request.QueryString["ID"] != null)
            {
                result = result + "?ID=" + request.QueryString["ID"];
            }
            if ((((((result.IndexOf("_layouts") > -1) || (result.IndexOf(".jpg") > -1)) || ((result.IndexOf(".gif") > -1) || (result.IndexOf(".png") > -1))) || (((result.IndexOf(".js") > -1) || (result.IndexOf(".css") > -1)) || ((result.IndexOf(".asmx") > -1) || (result.IndexOf(".ashx") > -1)))) || ((((result.IndexOf(".axd") > -1) || (result.IndexOf(".dll") > -1)) || ((result.IndexOf(".doc") > -1) || (result.IndexOf(".docx") > -1))) || (((result.IndexOf(".xsl") > -1) || (result.IndexOf("attachments") > -1)) || ((result.IndexOf(".xslx") > -1) || (result.IndexOf(Instance.TracficTrackingSummaryListName) > -1))))) || (result.IndexOf(Instance.TraficTrackingListName) > -1))
            {
                result = "";
            }
            if (result.EndsWith("default.aspx"))
            {
                result = result.Replace("default.aspx", "");
            }
            return result;
        }

        public decimal CountRate
        {
            get
            {
                if (this._DngCountRate.Equals((decimal) 1M))
                {
                    try
                    {
                        this._DngCountRate = Convert.ToDecimal(ConfigurationManager.AppSettings["CountRate"]);
                        if (this._DngCountRate < 1M)
                        {
                            this._DngCountRate = 1M;
                        }
                    }
                    catch
                    {
                    }
                }
                return this._DngCountRate;
            }
        }

        public long CurrentVisited
        {
            get
            {
                if (this._DngCurrentVisited.Equals((long) 0L))
                {
                    try
                    {
                        this._DngCurrentVisited = Convert.ToInt64(ConfigurationManager.AppSettings["CurrentVisited"]);
                    }
                    catch
                    {
                    }
                }
                return this._DngCurrentVisited;
            }
        }

        public static Global Instance
        {
            get
            {
                if (mInstance == null)
                {
                    Global tmp = new Global();
                    lock (SyncRoot)
                    {
                        mInstance = tmp;
                    }
                }
                return mInstance;
            }
        }

        public static DateTime Now
        {
            get
            {
                return DateTime.Now;
            }
        }

        public double RefreshRate
        {
            get
            {
                if (this._DngRefreshRate.Equals((double) 2.0))
                {
                    try
                    {
                        this._DngRefreshRate = Convert.ToDouble(ConfigurationManager.AppSettings["RefreshRate"]);
                        if (this._DngRefreshRate < 2.0)
                        {
                            this._DngRefreshRate = 2.0;
                        }
                    }
                    catch
                    {
                    }
                }
                return this._DngRefreshRate;
            }
        }

        public double RefreshTracking
        {
            get
            {
                if (this._DngRefreshTracking.Equals((double) 2.0))
                {
                    try
                    {
                        this._DngRefreshTracking = Convert.ToDouble(ConfigurationManager.AppSettings["RefreshTracking"]);
                        if (this._DngRefreshTracking < 1.0)
                        {
                            this._DngRefreshTracking = 1.0;
                        }
                    }
                    catch
                    {
                    }
                }
                return this._DngRefreshTracking;
            }
        }

        public string SiteUrl
        {
            get
            {
                if (this._SiteUrl.Equals(string.Empty))
                {
                    this._SiteUrl = ConfigurationManager.AppSettings["SiteUrl"];
                }
                return this._SiteUrl;
            }
        }

        public string TracficTrackingSummaryListName
        {
            get
            {
                if (this._DngTracficTrackingSummaryListName.Equals(string.Empty))
                {
                    this._DngTracficTrackingSummaryListName = ConfigurationManager.AppSettings["TracficTrackingSummaryListName"];
                }
                return this._DngTracficTrackingSummaryListName;
            }
        }

        public string TraficTrackingListName
        {
            get
            {
                if (this._TraficTrackingListName.Equals(string.Empty))
                {
                    this._TraficTrackingListName = ConfigurationManager.AppSettings["TraficTrackingListName"];
                }
                return this._TraficTrackingListName;
            }
        }

        public string VisitTrackingListName
        {
            get
            {
                if (this._DngVisitTrackingListName.Equals(string.Empty))
                {
                    this._DngVisitTrackingListName = ConfigurationManager.AppSettings["VisitTrackingListName"];
                }
                return this._DngVisitTrackingListName;
            }
        }
    }
}

